package com.beeantec.jnrerc.modules.member.service.impl;

import com.beeantec.jnrerc.core.constants.CacheKey;
import com.beeantec.jnrerc.core.utils.GsonUtils;
import com.beeantec.jnrerc.core.utils.RandomUtils;
import com.beeantec.jnrerc.core.utils.RedisUtils;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.entity.Member;
import com.beeantec.jnrerc.modules.member.service.MemberRegisterService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.net.URL;
import java.text.MessageFormat;

@Service
public class MemberRegisterServiceImpl implements MemberRegisterService {
    protected Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    RedisUtils redisUtils;

    @Autowired
    MemberService memberService;

    @Override
    public MemberVo register(String loginType, String id, String phone, String nickName, Integer gender, String avatarUrl) {
        Member member = new Member();
        member.setPhone(phone);
        member.setIdentifier(DigestUtils.md5DigestAsHex((phone + "JNRERCAPP").getBytes()));
        if (loginType.equals("wx")) {
            String wxUserInfoKey = MessageFormat.format(CacheKey.member_wxUserInfo, id);
            JsonObject userInfo = GsonUtils.fromJson(redisUtils.get(wxUserInfoKey).toString(), JsonObject.class);
            member.setWxOpenid(userInfo.get("openid").getAsString());
            member.setWxUnionid(userInfo.get("unionid").getAsString());
            member.setGender(userInfo.get("sex").getAsInt());
            member.setNickName(userInfo.get("nickname").getAsString());
            member.setDepId(0);
            member.setMemberType(0);
            member.setExp(0);
            member.setIntegral(0);
            member.setJobId(0);
            member.setCharm(0);
            String headimgurl = userInfo.get("headimgurl").getAsString();
            try {
                URL url = new URL(headimgurl);
                String http = url.toURI().getScheme();
                if ("http".equals(http)) {
                    headimgurl.replace("http", "https");
                }
            } catch (Exception e) {
                log.error("{}", e);
            }
            log.info("[headimgurl:{}]", headimgurl);
            member.setAvatarUrl(headimgurl);
        }else {
            member.setAvatarUrl("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F17%2F07%2F02%2Ff76042f8c7cd993efbc88687aaad0fe8.jpg&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641882677&t=b01968494a4325e3e19c9d71a02e4315");
            member.setNickName("用户" + RandomUtils.getRandom(4) + phone.substring(phone.length() - 4));
            member.setDepId(0);
            member.setMemberType(0);
            member.setExp(0);
            member.setIntegral(0);
            member.setJobId(0);
            member.setCharm(0);
        }
        if (memberService.saveOrUpdate(member)) {
            MemberVo memberVo = new MemberVo();
            BeanUtils.copyProperties(member, memberVo);
            return memberVo;
        }
        return null;
    }
}
